﻿from mgdnss import *

class ed(object):
    '''文档控制台封装类'''
    _ed = None
    @classmethod
    def GetAcadDocument(cls):
        doc = cls.Document
        if hasattr(doc, 'AcadDocument'):
            return doc.AcadDocument
        else:
            import clr
            clr.ImportExtensions(acap)
            return doc.GetAcadDocument()
    def __new__(cls):
        '''获取当前文档Editor'''
        cls._ed = acap.Application.DocumentManager.MdiActiveDocument.Editor

class _preperty(object):
    def __init__(self, name):
        self.__name__ = name
    def __get__(self, instance, owner):
        return getattr(owner._ed, self.__name__)
    def __repr__(self):
        return '<built-in method %s of Editor object at 0x%016X>' % (
            self.__name__, id(self))

'''封装aced.Editor类的属性到ed
并对函数进行特殊封装'''
for key in aced.Editor.__dict__:
    if key == '__doc__':
        ed.__doc__ = aced.Editor.__doc__
    else:
        t = type(aced.Editor.__dict__[key])
        setattr(ed, key, _preperty(key))

def showtime(func):
    from functools import wraps
    @wraps(func)
    def _func(*args, **kwargs):
        from datetime import datetime
        oldtime = datetime.now()
        res = func(*args, **kwargs)
        t = datetime.now() - oldtime
        print "函数名称:%s.%s" % (func.__module__, func.__name__)
        print "花费时间:%f秒" % t.total_seconds()
        return res
    return _func

def help(o):
    import pydoc
    pydoc.help(o)

class switch(object):
    '''
    case = switch(value)
    if case(somevalue) or
    if case[sometype] or
    if case > 0 or
    if case.match(func):
        dosomething'''
    def __init__(self, value):
        self.value = value
    def __call__(self, *args):
        return self.value in args
    def __getitem__(self, *args):
        return isinstance(self.value, args)
    def __lt__(self, other):
        return self.value < other
    def __gt__(self, other):
        return self.value > other
    def __le__(self, other):
        return self.value <= other
    def __ge__(self, other):
        return self.value >= other
    def match(self, func):
        return func(self.value)

def flatten(nested):
    '''展开生成器
    tuple(flatten([[1,'2'],3])) -> (1, '2', 3)'''
    case = switch(nested)
    if case[str]:
        yield nested
    else:
        try:
            for sublist in nested:
                for element in flatten(sublist):
                    yield element
        except TypeError:
            yield nested
